home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / IEditor / Generators / C / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-17  |  4.7 KB  |  190 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #include "DEV_IE:Generators/defs.h"
  15. #include "Protos.h"
  16.  
  17. extern struct Library *LibInit   ( __A0 BPTR );
  18. extern struct Library *LibOpen   ( __D0 long, __A0 struct Library * );
  19. extern long            LibClose  ( __A0 struct Library * );
  20. extern long            LibExpunge( __A0 struct Library * );
  21.  
  22. struct Library *LibBase = NULL;
  23.  
  24. long SysBase        = NULL;
  25. long DOSBase        = NULL;
  26. long IntuitionBase  = NULL;
  27. long GfxBase        = NULL;
  28. long ReqToolsBase   = NULL;
  29. long GadToolsBase   = NULL;
  30. BPTR SegList        = 0;
  31.  
  32. /*
  33.  *    The Initialization routine is given only a seglist pointer.  Since
  34.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  35.  *    and return either NULL or the library pointer.  Exec has Forbid()
  36.  *    for us during the call.
  37.  */
  38.  
  39. struct Library *LibInit( __A0 BPTR segment )
  40. {
  41.  
  42.     struct Library *lib = NULL;
  43.  
  44.     static const long Vectors[] = {
  45.  
  46.     (long)ALibOpen,
  47.     (long)ALibClose,
  48.     (long)ALibExpunge,
  49.     (long)ALibReserved,
  50.  
  51.     (long)OpenFiles,
  52.     (long)CloseFiles,
  53.     (long)WriteHeaders,
  54.     (long)WriteVars,
  55.     (long)WriteData,
  56.     (long)WriteStrings,
  57.     (long)WriteChipData,
  58.     (long)WriteCode,
  59.     (long)Config,
  60.     -1
  61.     };
  62.  
  63.     SysBase = *(long *)4;
  64.  
  65.     if( DOSBase = OpenLibrary( "dos.library", 36 )) {
  66.     if( IntuitionBase = OpenLibrary( "intuition.library", 36 )) {
  67.         if( GfxBase = OpenLibrary( "graphics.library", 36 )) {
  68.         if( GadToolsBase = OpenLibrary( "gadtools.library", 36 )) {
  69.             if( ReqToolsBase = OpenLibrary( "reqtools.library", 37 )) {
  70.  
  71.             if( LibBase = lib = MakeLibrary( (APTR)Vectors, NULL, NULL, sizeof(struct Generator), NULL )) {
  72.  
  73.                 lib->lib_Node.ln_Type = NT_LIBRARY;
  74.                 lib->lib_Node.ln_Name = LibName;
  75.                 lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  76.                 lib->lib_Version      = 37;
  77.                 lib->lib_Revision     = 18;
  78.                 lib->lib_IdString     = (APTR)LibId;
  79.  
  80.                 ((struct Generator *)lib)->Ext      = "c";
  81.                 ((struct Generator *)lib)->Pattern  = "#?.c";
  82.  
  83.                 LoadPrefs();
  84.  
  85.                 SegList = segment;
  86.  
  87.                 AddLibrary( lib );
  88.             }
  89.  
  90.             }
  91.         }
  92.         }
  93.     }
  94.     }
  95.  
  96.     return( lib );
  97. }
  98.  
  99. /*
  100.  *    Open is given the library pointer and the version request.  Either
  101.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  102.  *    Exec has Forbid() for us during the call.
  103.  */
  104.  
  105. struct Library *LibOpen( __D0 long version, __A0 struct Library *lib )
  106. {
  107.     ++lib->lib_OpenCnt;
  108.  
  109.     lib->lib_Flags &= ~LIBF_DELEXP;
  110.  
  111.     return( lib );
  112. }
  113.  
  114. /*
  115.  *    Close is given the library pointer and the version request.  Be sure
  116.  *    not to decrement the open count if already zero.  If the open count
  117.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  118.  *    and return the seglist.  Otherwise we return NULL.
  119.  *
  120.  *    Note that this routine never sets LIBF_DELEXP on its own.
  121.  *
  122.  *    Exec has Forbid() for us during the call.
  123.  */
  124.  
  125. long LibClose( __A0 struct Library *lib )
  126. {
  127.     if( lib->lib_OpenCnt && --lib->lib_OpenCnt )
  128.     return( NULL );
  129.  
  130.     if( lib->lib_Flags & LIBF_DELEXP )
  131.     return( LibExpunge( lib ));
  132.  
  133.     return( NULL );
  134. }
  135.  
  136. /*
  137.  *    We expunge the library and return the Seglist ONLY if the open count
  138.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  139.  *    flag and return NULL.
  140.  *
  141.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  142.  *    might be called from the memory allocator and thus we CANNOT DO A
  143.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  144.  *
  145.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  146.  *    therefore freeze if we called it ourselves.  As far as I can tell
  147.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  148.  *    below.
  149.  */
  150.  
  151. long LibExpunge( __A0 struct Library *lib )
  152. {
  153.     if( lib->lib_OpenCnt ) {
  154.  
  155.     lib->lib_Flags |= LIBF_DELEXP;
  156.     return( NULL );
  157.     }
  158.  
  159.     Remove( &lib->lib_Node );
  160.  
  161.     FreeMem(( char * )lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize );
  162.  
  163.     if( DOSBase ) {
  164.     CloseLibrary( (struct Library *)DOSBase );
  165.     DOSBase = NULL;
  166.     }
  167.  
  168.     if( IntuitionBase ) {
  169.     CloseLibrary( (struct Library *)IntuitionBase );
  170.     IntuitionBase = NULL;
  171.     }
  172.  
  173.     if( GfxBase ) {
  174.     CloseLibrary( (struct Library *)GfxBase );
  175.     GfxBase = NULL;
  176.     }
  177.  
  178.     if( GadToolsBase ) {
  179.     CloseLibrary( (struct Library *)GadToolsBase );
  180.     GadToolsBase = NULL;
  181.     }
  182.  
  183.     if( ReqToolsBase ) {
  184.     CloseLibrary( (struct Library *)ReqToolsBase );
  185.     ReqToolsBase = NULL;
  186.     }
  187.  
  188.     return(( long )SegList );
  189. }
  190.